package com.albaba.designpattern.prototype.text_003;

import java.util.UUID;
import java.util.concurrent.*;

/**
 * @author zx
 * @version 1.0
 * @description 业务场景
 * @date 2025/2/19 22:22
 */
public class Client {

    // 发送邮件的数量
    private static int count = 100;

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,
            10,
            30,
            TimeUnit.MINUTES,
            new LinkedBlockingDeque<>(),
            new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r);
                    thread.setName("sendMail-" + count);
                    return thread;
                }
            },
            new ThreadPoolExecutor.DiscardOldestPolicy()
    );

    // 发送邮件的功能
    public static void sendMail(Mail mail) {
        System.out.println("标题：" + mail.getSubject() +
                "收件人：" + mail.getReceiver()
                + "\t 发送成功");
    }


    public static void main(String[] args) throws Exception {
        Mail mail = new Mail(new AdvTemplate());
        mail.setTail("XX银行版权所有");
        mail.setSubject("来自XX银行");
        mail.setAppellation("尊敬的XX银行用户");
        mail.setContext("这是一封来自XX银行公司的邮件");
        mail.setReceiver(UUID.randomUUID().toString());

        executor.submit(()->{
            for (int i = 0; i < 6; i++) {
                sendMail(mail);
            }

        });

    }

}
